﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmProvider : Form
    {
        DialogResult dlgResult;
        ProviderInfo providerInfo;
        public frmProvider(int _provider_ID)
        {
            InitializeComponent();
            providerInfo = new ProviderInfo();
            txtCode.Text = UISetting.AutoFillProviderCode();
            if (_provider_ID > 0)
            {
                providerInfo = ProviderDB.GetInfo(_provider_ID);
                
                InfoToForm();
            }
            providerInfo.Provider_ID = _provider_ID;
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        private void FormToInfo()
        {
            providerInfo.Provider_Code = txtCode.Text;
            providerInfo.Provider_Name = txtName.Text;
            providerInfo.Provider_Address = txtAddress.Text;
            providerInfo.Provider_TaxNo = txtTaxNo.Text;
            providerInfo.Provider_Phone = txtPhone.Text;
            providerInfo.Provider_Contact = txtContact.Text;

        }
        private void InfoToForm()
        {
            txtCode.Text = providerInfo.Provider_Code;
            txtName.Text = providerInfo.Provider_Name;
            txtAddress.Text = providerInfo.Provider_Address;
            txtTaxNo.Text = providerInfo.Provider_TaxNo;
            txtPhone.Text = providerInfo.Provider_Phone;
            txtContact.Text = providerInfo.Provider_Contact;
        }
        private bool CheckValid()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Tên NCC trống! Hãy nhập lại", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            FormToInfo();
            if (providerInfo.Provider_ID > 0)//Update
            {
                if (ProviderDB.Exist(providerInfo.Provider_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (ProviderDB.Update(providerInfo))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else //Create
            {
                if (ProviderDB.Exist(providerInfo.Provider_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                providerInfo.Provider_ID = ProviderDB.Insert(providerInfo);
                if (providerInfo.Provider_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Create_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmProvider_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            providerInfo.Provider_ID = 0;
            txtCode.Text = UISetting.AutoFillProviderCode();
            txtName.Text = "";
            txtAddress.Text = "";
            txtTaxNo.Text = "";
            txtPhone.Text = "";
            txtContact.Text = "";
        }

        private void frmprovider_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnSaveToNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }

        private void btnSaveToNew_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
            btnNew_Click(null, null);
        }
    }
}